<?php

class Feedback extends X3_Module_Table {

    public $encoding = 'UTF-8';
    public $tableName = 'data_feedback';
    public $_fields = array(
        'id' => array('integer[10]', 'unsigned', 'primary', 'auto_increment'),
        'name' => array('string[255]'),
        'email' => array('email'),
        'text' => array('string[1024]'),
    );
       
    public static function newInstance($class = __CLASS__) {
        return parent::newInstance($class);
    }

    public static function getInstance($class = __CLASS__) {
        return parent::getInstance($class);
    }

    public static function get($arr=array(), $single = false, $class = __CLASS__) {
        return parent::get($arr, $single, $class);
    }

    public static function getByPk($pk, $class = __CLASS__) {
        return parent::getByPk($pk, $class);
    }

    public function moduleTitle() {
        return 'Отзывы';
    }
    
    public function fieldNames() {
        return array(
            'name' => 'Ваше имя',
            'email' => 'Ваш Email',
            'text' => 'Текст письма',
        );
    }

    public function actionIndex() {
        if(!X3::user()->isGuest())
            $this->redirect('/');
        $error = false;
        $model = new Feedback();

        if(isset($_POST['Feedback'])){
            $model->getTable()->acquire($_POST['Feedback']);
            if($model->save()){
                Notify::sendMail('Feedback',$model->getAttributes());
                if(IS_AJAX)
                    die(json_encode(array('status'=>'OK','message'=>'Сообщение успешно отправлено!')));
                $this->refresh();
            }elseif(IS_AJAX)
                die(json_encode(array('status'=>'ERROR','message'=>  X3_Html::errorSummary($model))));
        }
        if(IS_AJAX)
            $this->template->layout = null;
        $this->template->render('index',array('error'=>$error,'model'=>$model));
    }
}